//
//  main.cpp
//  模板
//
//  Created by linjinzhu on 2017/7/14.
//  Copyright © 2017年 linjinzhu. All rights reserved.
//

#include <iostream>
#include <vector>

using namespace std;

template <typename T>
inline T const & Max(T const &a, T const &b) {
    return a > b ? a : b;
}

template <class T>
class Stack {
private:
    vector<T> elems;// 元素

public:
    void push(T const &elem);
    void pop();
    T top() const; // 返回栈顶元素
    bool empty() {
        return elems.empty();
    }
};

template <class T>
void Stack<T>::push(const T &elem) {
    elems.push_back(elem);
}

template <class T>
void Stack<T>::pop() {
    if (elems.empty()) {
        throw out_of_range("Stack<>:: pop empty stack.");
    }
    elems.pop_back();
}

template <class T>
T Stack<T>::top() const {
    if (elems.empty()) {
        throw out_of_range("Stack<>:: pop empty stack.");
    }

    return elems.back();
}

int main(int argc, const char * argv[]) {

    int i = 1, j = 2;
    cout << Max(i, j) << endl;

    double m = 3, n = 6;
    cout << Max(m, n) << endl;

    char *x = "name", *y = "word";
    cout << Max(x, y) << endl;


    try {
        Stack<int> intStack;
        Stack<string> stringStack;

        // 整形操作
        intStack.push(10);
        cout << intStack.top() << endl;

        // 操作stirng型的栈
        stringStack.push("hello");
        cout << stringStack.top() << endl;

        stringStack.pop();
        stringStack.pop();



    } catch (exception const &e) {
        cerr << e.what() << endl;
    }

    return 0;
}
